package com.ruoyi.web.controller.chat.util;

import com.lzhpo.chatgpt.entity.chat.ChatCompletionRequest;
import com.lzhpo.chatgpt.entity.chat.ChatCompletionResponse;
import com.ruoyi.web.core.config.ChatModel;

import java.util.Random;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-05-24 18:06:58
 */
public class ChangeUtil {

    public static ChangeDTO change(String realModelName,String openAiKey) {
        ChangeDTO changeDTO = new ChangeDTO();
        changeDTO.setRealModelName(realModelName);

        if (realModelName.startsWith(ChatModel.GPT_4.getModelShortName())) {
            if(!realModelName.equalsIgnoreCase(ChatModel.GPT_4.getModelShortName())){
                throw new IllegalArgumentException("不支持的gpt4模型");
            }
//            changeDTO.setChange(true);
//            changeDTO.setChangeModelName("gpt-3.5-turbo-16k");


            Random r = new Random();
            // 生成[0,10]区间的整数
            int i1 = r.nextInt(10);
            if (i1 % 3 == 0) {

            } else {
                changeDTO.setChange(false);
                changeDTO.setChangeModelName(realModelName);
            }
        } else {
            changeDTO.setChange(false);
            changeDTO.setChangeModelName(realModelName);
        }
        return changeDTO;
    }

    public static void changeRequest(ChatCompletionRequest request, ChangeDTO dto) {
        if (dto.getChange()) {
            request.setModel(dto.getChangeModelName());
        }
    }

    public static void changeResponse(ChatCompletionResponse response, ChangeDTO dto) {
        if (dto.getChange()) {
            response.setModel(ChatModel.queryByModelName(dto.getRealModelName()).getModelLongName());
        }
    }
}
